<template>
  <div class="container">
    <div>
      <el-row :gutter="20">
        <el-col :span="5">
          <el-form ref="form" :model="form">
            <el-form-item label="案件来源：">
              <el-input v-model="select.name" placeholder="请选择" style="width:150px;"></el-input>
            </el-form-item>
          </el-form>
          
          <el-form ref="form" :model="form">
            <el-form-item label="考核标准：">
              <el-input v-model="select.name" placeholder="请选择" style="width:150px;"></el-input>
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :span="8">
          <el-form ref="form" :model="form">
            <el-form-item label="案件状态：">
              <el-input v-model="select.name" placeholder="请选择" style="width:150px;"></el-input>
            </el-form-item>
          </el-form>
                  <el-form ref="form" :model="form">
            <el-form-item label="考核项目：">
              <el-input v-model="select.name" placeholder="请选择" style="width:150px;"></el-input>
            </el-form-item>
          </el-form>
          </el-col>
        <el-col :span="8">
  
          <el-form ref="form" :model="form" style="float:left">
            <el-form-item>
              <el-button size="mini" type="primary" @click="screeningData">查 询</el-button>
              <el-button size="mini" type="success" @click="handleAdd">新 增</el-button>
              <el-button style="padding:0;border:0">
              <el-upload
                  action="/上传文件的接口"
                  :on-change="onChange"
                  :auto-upload="false"
                  :show-file-list="false"
                  accept=".xls, .xlsx"
                  class="upload"
              >
                <el-button size="mini" type="success" >导 入</el-button>
              </el-upload>
              </el-button>
              <el-button size="mini" type="warning" @click="exportExcel">导 出</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <el-table highlight-current-row :data="items.slice((currentPage-1)*pageSize,currentPage*pageSize)">
      <!-- <el-table-column  prop="ID" label="编号" align="center" ></el-table-column> -->
      <!-- <el-table-column  prop="XZ" label="考核细则" align="center" ></el-table-column> -->
      <!-- 案件来源、对考核任务编号、考核项目、考核对象、需整改内容、整改时限、整改单位、整改结果、案件状态。 -->
      <el-table-column  prop="Come" label="案件来源" align="center" ></el-table-column>
      <el-table-column  prop="XM" label="考核项目" align="center" ></el-table-column>
      <el-table-column  prop="STR" label="考核标准" align="center" ></el-table-column>
      <el-table-column  prop="" label="考核对象" align="center" ></el-table-column>
      <el-table-column  prop="NR" label="需整改内容" align="center" ></el-table-column>
      
      <el-table-column  prop="" label="整改时限" align="center" >1天</el-table-column>
      <el-table-column  prop="sdw" label="整改单位" align="center" ></el-table-column>
      <el-table-column  prop="" label="整改结果" align="center" ></el-table-column>
      <el-table-column  prop="st" label="案件状态" align="center" ></el-table-column>
      <el-table-column label="操作" width="300" align="center" >
        <template slot-scope="scope">
          <el-button type="text" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            type="text"
            @click="handleDelete(scope.$index, scope.row)"
            style="color: red"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        background
        layout="total, sizes, prev, pager, next, jumper"
        :current-page="currentPage"
        :page-size="pageSize"
        :total="items.length"
        @size-change="handleSizeChange"
        @current-change="handlePageChange"
      ></el-pagination>
    </div>
    <!-- 弹出框 -->
    <el-dialog
      :title="addDialogTitle"
      :visible.sync="isVisibleEdit"
      width="50%"
    >
      <el-form
        ref="form" 
        :model="form"
        label-width="100px"
        class="flex"
      >
      <!-- 案件来源、案件登记时间、所属区域、案件类型、案件内容等信息，系统自动匹配“整改时限、整改单位”、整改结果、案件状态。 -->

        <el-form-item label="案件来源" class="wid50">
          <el-select v-model="form.XM" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.XM"
              :label="item.XM"
              :value="item.XM">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域" class="wid50">
          <el-select v-model="form.XM" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.XM"
              :label="item.XM"
              :value="item.XM">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核项目" class="wid50">
          <el-select v-model="form.XM" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.XM"
              :label="item.XM"
              :value="item.XM">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="考核标准" class="wid50">
          <el-select v-model="form.XM" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.XM"
              :label="item.XM"
              :value="item.XM">
            </el-option>
          </el-select>
        </el-form-item>
   
         
        <el-form-item label="整改单位" class="wid50">
          <el-select v-model="form.XM" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.XM"
              :label="item.XM"
              :value="item.XM">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="整改时限" class="wid50">
          <el-input
            v-model="form.FZ"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>
       <el-form-item label="内容描述" class="wid50">
          <el-input
            v-model="form.NR"
            placeholder="请输入"
            class="handle-input mr10"
          ></el-input>
        </el-form-item>       
       </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="isVisibleEdit = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import '@/assets/css/common.css'

import export2Excel from '@/utils/exportExcel/index.js'
import OSS from 'ali-oss';
import { time } from 'echarts';
export default {
  data () {

    return {
      clientData:[],
      currentPage: 1, //初始页
      pageSize: 10,    //    每页的数据
      originalData: [],
      select: {
        CPH: ''
      },
      isVisibleEdit: false,
      form: {
        WaterCPH: "",
        WaterTime: "",
        CreateTime: "",
        WaterNumber: "",
        WaterLocation: "",
        WaterCphType:"",
        WaterPeople: "",
        WaterPeopleID: "",
      },
      items: [
        {sdw:'博德尔公司',st:'已审核',Come:'市民热线',STR:'道路积尘',NR:'道路脏',XM:'机扫道路保洁',FZ:'10分',SX:'5天'},
        {sdw:'博德尔公司',st:'已审核',Come:'被动接收',STR:'道路积尘',NR:'人行道脏',XM:'人工道路保洁',FZ:'15分',SX:'10天'},
        {sdw:'博德尔公司',st:'已审核',Come:'被动接收',STR:'渗滤液',NR:'转运站渗滤液不达标',XM:'垃圾收运',FZ:'5分',SX:'3天'},
        {sdw:'博德尔公司',st:'已审核',Come:'日常检查',STR:'七净六无',NR:'公厕脏',XM:'公厕保洁',FZ:'20分',SX:'10天'},
        {sdw:'博德尔公司',st:'已审核',Come:'抽查考核',STR:'卫生质量差',NR:'果皮箱脏',XM:'垃圾收运',FZ:'5分',SX:'3天'},
        {sdw:'博德尔公司',st:'已审核',Come:'集中评比',STR:'卫生质量差',NR:'垃圾箱脏',XM:'垃圾收运',FZ:'5分',SX:'3天'},
      ],
      options: [{
          XM: '道路清扫保洁'
        }, {
          XM: '垃圾收运'
        },{
          XM: '垃圾转运'
        },{
          XM: '公厕保洁作业质量'
        }, {
          XM: '环卫设施养护'
        }, {
          XM: '环卫作业公司'
        }],
      pickerOptions: {
          shortcuts: [{
            text: '最近一周',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近六个月',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 180);
              picker.$emit('pick', [start, end]);
            }
          },{
            text: '最近一年',
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 365);
              picker.$emit('pick', [start, end]);
            }
          },]
        },
        value2: ''
    }
  },
  created () {
    if (localStorage.getItem('CTYPE') == '0') {
      this.rolelevel.shift()
    } else if (localStorage.getItem('CTYPE') != '-1') {
      this.rolelevel = []
    }
    // this.getClientRoleList()
  },
  mounted () {
    // this.getlistdata()
    // this.getClientRoleList ()
    this.originalData = data
    this.screeningData()
  },
  methods: {

    // 导入Excel
    onChange (file, fileList) {
      //  console.log(fileList);
      this.fileData = file; // 保存当前选择文件
      this.readExcel(); // 调用读取数据的方法
    },
    // 读取数据
    readExcel (e) {
      let that = this;
      const files = that.fileData;
      console.log(files);
      if (!files) {
        //如果没有文件
        return false;
      } else if (!/\.(xls|xlsx)$/.test(files.name.toLowerCase())) {
        this.$message.error("上传格式不正确，请上传xls或者xlsx格式");
        return false;
      }
      const fileReader = new FileReader();
      console.log(fileReader)
      fileReader.onload = ev => {
        try {
          const data = ev.target.result;
          //  console.log(ev.target)
          //  console.log(this);
          const workbook = this.XLSX.read(data, {
            type: "binary"// 以字符编码的方式解析
          });
          
          if (workbook.SheetNames.length >= 1) {
            //  this.$message({
            //    message: "导入数据表格成功",
            //    showClose: true,
            //    type: "success"
            //  });

            const wsname = workbook.SheetNames[0]; //取第一张表
            const ws = this.XLSX.utils.sheet_to_json(workbook.Sheets[wsname]); //生成json表格内容
            console.log(ws)
            this.inputExcelDataLength = ws.length
            this.fullscreenLoading = this.$loading({
              lock: true,
              text: 'Loading',
              spinner: 'el-icon-loading',
              background: 'rgba(0, 0, 0, 0.7)'
            })
            for (let i = 0, len = ws.length; i < len; i++) {
              var item = ws[i]
              //   var sheetData = {
              //     VID: item['居民电话'],
              //     RUBBISH_NAME: item["垃圾品名"],
              //     WEIGHT: item["垃圾称重"],
              //     photo: '',
              //     LONGITUDE: 0,
              //     LATITUDE: 0
              //   }
              //   let exceldata =  []
              //   exceldata.push(sheetData)
              this.addWeight(item)
            }
          } else {
            this.$message.error("未读取到表格内容");
          }
        } catch (e) {
          return false;
        }
      };
      // 如果为原生 input 则应是 files[0]
      fileReader.readAsBinaryString(files.raw);
    },
    async addWeight (item) {
     this.items.splice(7,0,{NR: item['考核内容'],XM: item['考核项目'],FZ: item['扣分分值'],SX: item['整改时限']})
      this.$message.success('表格文件导入成功');
      this.fullscreenLoading.close()
    },
    // 导出Excel
    exportExcel () {
      let excelName = '考核细则记录'
      const tHeader = ['考核内容', '考核项目', '扣分分值', '整改时限']
      const filterVal = ['NR', 'XM', 'FZ', 'SX']
      export2Excel(tHeader, filterVal, this.items, excelName)
    },
    // 打开新增窗口
    handleAdd () {
      this.addDialogTitle = '新增案件'
      //清空编辑框
      this.form = {}
      //展示窗口
      this.isVisibleEdit = true;
      // this.idEdit = false;
    },
    // 打开编辑窗口
    handleEdit (index, row) {
      this.form = row;
      this.addDialogTitle = '编辑考核标准'
      this.isVisibleEdit = true;
    },
    // 点击保存，判断是新增还是编辑操作
    handleSubmit () {
      if (this.addDialogTitle == '新增考核细则信息') {
        this.saveAdd()
      } else {
        this.saveEdit()
      }
    },
    // 新增保存
    async saveAdd(){
      this.$set(this.items,7,{NR: this.form.NR,XM: this.form.XM,FZ: this.form.FZ,SX: this.form.SX})
      this.items.splice(7,0)
      this.$message.success('新增成功');
      this.isVisibleEdit =false
      },
    // 保存编辑
    async saveEdit(){
          this.items.NR = this.form.NR
          this.items.XM = this.form.XM
          this.items.FZ = this.form.FZ
          this.items.SX = this.form.SX
          this.$message.success('编辑成功');
          this.isVisibleEdit =false
        },
      // 删除车辆信息
      async handleDelete (index, row) {
        const confirmResult = await this.$confirm('确定删除？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).catch(err => err)
        if (confirmResult == 'confirm') {
          this.items.splice(row.ID-1,1)
          this.$message.success('删除成功')
          this.isVisibleEdit =false
        }else{
          this.$message.info('取消删除')
          this.isVisibleEdit = false
        }
      
      },
    // 模糊查询
    screeningData () {
      if (this.select.name) this.items = this.items.filter(item => item.NR.includes(this.select.name))
    },
    // 分页导航
    handlePageChange (currentPage) {
      this.currentPage = currentPage;
      // console.log(this.currentPage)
      // this.$set(this.query, 'pageIndex', val);
      // this.getlistdata();
    },
    handleSizeChange (size) {
      this.pageSize = size;
      console.log(this.pageSize)
    }
  }
}
</script>
<style>
.content {
  overflow-y: scroll;
}
.el-col-6 {
  width: 350px;
  min-height: 50px;
}
.el-table th {
  height: 40px;
}
.el-form-item label {
  width: 80px;
}

</style>